var del_case_ids = new Array();
var owners;
var toPage = 1;
var totalPage = 1;
$(function() {
	initSearch();
	initTable();
	initDialog();
});

function initSearch() {
	toPage = 1;
	$('.J_Submit').on('click', function() {
//		$("#loading2").show();//显示查询时的gif图片
		var doText = $("#doInput").val();
		if (doText == "") {
//			$("#loading2").hide();
			return;
		}
	});
	$(".prev").click(function () {
			//mark
			if (toPage > 1){
				toPage--;
				searchBy();
			}
		});
	$(".next").click(function () {
		if (!isEnd){
			toPage++;
			searchBy();
			} else {
				showEndInfo();
			}
	 });
	$("#dt_row").change(function () {
		toPage = 1 ;
		searchBy();
	});
}


//初始化表格
function initTable() {
	fetchTable('','','','',0);
	//全选/取消全部 
	$("#checkAllChange").click(function() { 
	if (this.checked == true) { 
	del_case_ids = [];
	$(".casestable").each(function() { 
	this.checked = true; 
	del_case_ids.push(this.value);
	}); 
	} else { 
	$(".casestable").each(function() { 
	this.checked = false; 
	}); 
	del_case_ids = [];
	} 
	});
}


function fetchTable(name,classes,scenes,owners,level) {
	//$('#loading').show();
    isEnd = true ;
    var searchvalue = $("#doInput").val();
    var pageSize = $('#dt_row').val();
    owners=owners;
	$.ajax({
		url : '/smarttest-frontstage/AutoProtocol/search/table.action',
		dataType : 'json',
		method : 'POST',
		data : {name:name,classes:classes,scenes:scenes,owners:owners,searchvalue:searchvalue,pageSize:pageSize,toPage:toPage,level:level},
		success : function(data) {
			del_case_ids=[];
			$("#tbody").html("");
			var msg = data.rows;
			var i = 0;
			var html = '';
	  			$.each(msg, function(k, v){
	  				var item = msg[i];
                    if(i%2===0){
                    	html += '<tr bgcolor="#DFEBF2">';
                    }else{
                    	html += '<tr>';
                    }
                    html += '<td>' + "<input type=\"checkbox\" class=\"casestable\" onclick=\"javascript:singleCheck(this);\" value=\""+item.id+"\"/>" + '</td>';
					html += '<td>' + item.name + '</td>';
					html += '<td>' + item.clazz + '</td>';
					html += '<td>' + item.scens + '</td>';
					html += '<td>' + item.owner + '</td>';
					html += '<td>' + item.remark + '</td>';
					html += '<td>' + item.level + '</td>';
					html += '<td>' + getNowFormatDate(item.created) + '</td>';
					html += '<td>' + getNowFormatDate(item.updated) + '</td>';
					html += '<td>' + "<input type=\"button\" onclick=\"executeCase(this);\" class=\"btn btn-primary  btn-sm\"  id=\""+item.id+"\" value=执行用例>" + '</td>';
					html += '</tr>';
					i++;
	  			});
	  			$("#tbody").append(html);
			$('#loading').hide();
		}
	});
}

function initDialog(){
	$('#addCase').click(function() {
		addCase();
	});
}

function initmodifyDialog(){
	$('#addCase').click(function() {
		addCase();
	});
}

function getAddDialog(){
	$("#caseid").val(-1);
	$('#casemodal').modal();
}

function addCase(){
	var id = $("#caseid").val();
	var name = $("#name").val();
	var clazz = $("#clazz").val();
	var scenes = $("#scenes").val();
	var owner = $("#owner").val();
	var remark = $("#remark").val();
	var level = $("#caseLevel").val();
	$.ajax({
		url : '/smarttest-frontstage/AutoProtocol/case/add.action',
		dataType : 'json',
		method : 'POST',
		data : {id:id,name:name,clazz:clazz,scenes:scenes,owner:owner,remark:remark,level:level},
		complete : function(data) {
			alertDialog('增加/修改用例','操作成功');
			$('#casemodal').modal('hide');
			fetchTable('','','',owner,0);
		}
	});
}

function modifyCases(){
	if(del_case_ids.length!=1){
		alertDialog('修改用例','请选择单个设备进行修改');
		return;
	}
	var dd = $(":checkbox[value="+del_case_ids[0]+"]");
	var id = del_case_ids[0];
	var trselected = dd.parent().parent();
	var name = trselected.children("td:eq(1)").text();
	var clazz = trselected.children("td:eq(2)").text();
	var scenes = trselected.children("td:eq(3)").text();
	var owner = trselected.children("td:eq(4)").text();
	var remark = trselected.children("td:eq(5)").text();
	var caselevel =  trselected.children("td:eq(6)").text();
	$("#caseid").val(id);
	$("#name").val(name);
	$("#clazz").val(clazz);
	$("#scenes").val(scenes);
	$("#owner").val(owner);
	$("#remark").val(remark);
	$("#caseLevel").val(caselevel);
	$('#casemodal').modal();
}

function singleCheck(checkbox){
	if(checkbox.checked){
		if(-1==$.inArray(checkbox.value, del_case_ids)){
			del_case_ids.push(checkbox.value);
		}
	}else{
		var i = $.inArray(checkbox.value, del_case_ids);
		if(i>-1){
			del_case_ids.splice(i, 1);
		}
	}
}


function deleteCase(){
	if(del_case_ids.length<1){
		alertDialog("删除用例","请选择一个用例再删除");
		return;
	}
	var info='';
	for (var i = 0; i < del_case_ids.length; i++) {
	     // 如果i+1等于选项长度则取值后添加空字符串，否则为逗号
	     info = (info + del_case_ids[i]) + (((i + 1)== del_case_ids.length) ? '':','); 
	}
	$.SmartMessageBox({
		title : "<i class='fa fa-sign-out txt-color-orangeDark'></i>Delete Case<span class='txt-color-orangeDark'><strong>" + $('#show-shortcut').text() + "</strong></span> ?",
		content : "确认要删除相应用例？请谨慎操作",
		buttons : '[No][Yes]'

	}, function(ButtonPressed) {
		if (ButtonPressed == "Yes") {
			$.ajax({
				url : '/smarttest-frontstage/AutoProtocol/case/delete.action',
				dataType : 'json',
				method : 'POST',
				data : {idsStr : info},
				success : function(data) {
				fetchTable('','','','',0);
				}
			});
		}});
}


function showEndInfo () {
	$('#reach_end').show();
	setTimeout("$('#reach_end').hide();",3000);
}


function getNowFormatDate(ns) {
    var date = new Date(ns);
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
}

function search(){
	  var level = $("#caseLevelSearch").val();
	  if (event.keyCode == 13)
	  {
	    event.returnValue=false;
	    event.cancel = true;
	    fetchTable('','','','',level);
	  }
}

function alertDialog(title,content){
	$.SmartMessageBox({
		title : "<i class='fa fa-sign-out txt-color-orangeDark'></i>"+title+"<span class='txt-color-orangeDark'><strong>" + $('#show-shortcut').text() + "</strong></span>",
		content : content,
		buttons : '[确定]'

	}, function(ButtonPressed) {
		if (ButtonPressed == "确定") {
	    }
	});
}

function executeCase(each){
	alert(each.id);
}











	

	
	
